﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class ProjectsDAO : IProjectsDAO
    {
        public bool DeleteProjects(Projects clsProjects)
        {
            return clsProjects.Save("D");
        }

        public IList<Projects> GetProjects(Projects clsProjects)
        {
            DataTable dt = Db.GetDataTable(clsProjects.CreateGetCommandParameters());
            clsProjects.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("Projects doesn't exist");
            }
            IList<Projects> list = new List<Projects>();
            list.Add(clsProjects);
            return list;
        }

        public IList<Projects> GetProjects(string Projects)
        {
            Projects clsProjects = new Projects();
            clsProjects._ProjectCode = Projects;
            return this.GetProjects(clsProjects);
        }

        public IList<Projects> GetsProjects()
        {
            Projects clsProjects = new Projects();
            IDataReader dtReader = Db.GetDataReader(clsProjects.CreateGetsCommandParameters());
            IList<Projects> list = new List<Projects>();
            while (dtReader.Read())
            {
                clsProjects = new Projects();
                clsProjects.LoadDataToCollection(dtReader);
                list.Add(clsProjects);
            }
            return list;
        }

        public DataTable GetsProjects(string SPName, DbParameter[] param)
        {
            Projects clsProjects = new Projects();
            return Db.GetDataTable(clsProjects.GetsDataReader(SPName, param));
        }

        public bool InsertProjects(Projects clsProjects)
        {
            return clsProjects.Save("I");
        }

        public int InsertProjectsReturnIdentity(Projects clsProjects)
        {
            return clsProjects.Save("I", true);
        }

        public string InsertProjectsReturnOutput(Projects clsProjects)
        {
            return clsProjects.Save("I", "@ProjectsID");
        }

        public IList<Projects> ListProjects(Projects clsProjects)
        {
            IDataReader dtReader = Db.GetDataReader(clsProjects.CreateListCommandParameters());
            IList<Projects> list = new List<Projects>();
            while (dtReader.Read())
            {
                Projects clsProjectss = new Projects();
                clsProjectss.LoadDataToCollection(dtReader);
                list.Add(clsProjectss);
            }
            return list;
        }

        public IList<Projects> ListProjects(string Projects)
        {
            Projects clsProjects = new Projects();
            clsProjects._ProjectCode = Projects;
            return this.ListProjects(clsProjects);
        }

        public bool UpdateProjects(Projects clsProjects)
        {
            return clsProjects.Save("U");
        }

        public int UpdateProjects(string SPName, DbParameter[] param)
        {
            Projects clsProjects = new Projects();
            return Db.ExecuteNonQuery(clsProjects.UpdateQuery(SPName, param));
        }
    }
}

